<?php

class Statistics {
	
	public $mec;
	public $igrac;
	public $licne;
	public $ft;
	public $fg;
	public $threept;
	public $mft;
	public $mfg;
	public $mtp;
	public $orbd;
	public $drbd;
	public $blk;
	public $ast;
	public $stl;

	public $pts;
	public $totalRbd;
	public $BLindex;
	
		
	// constructor
	
	public function __construct($i = 0, $m = 0) {
		
		$this->igrac = $i;
		$this->mec = $m;
		
		if ($i!=0 && $m!=0) {
			$n = $this->getfromdb();
			$this->licne = intval($n['licne']);
			$this->ft = intval($n['ft']);
			$this->fg = intval($n['fg']);
			$this->threept = intval($n['3pt']);
			$this->mft = intval($n['mft']);
			$this->mfg = intval($n['mfg']);
			$this->mtp = intval($n['m3p']);
			$this->orbd = intval($n['orbd']);
			$this->drbd = intval($n['drbd']);
			$this->blk = intval($n['blk']);
			$this->ast = intval($n['ast']);
			$this->stl = intval($n['stl']);
		}
		else {
			$this->licne = 0;
			$this->ft = 0;
			$this->fg = 0;
			$this->threept = 0;
			$this->orbd = 0;
			$this->drbd = 0;
			$this->blk = 0;
			$this->ast = 0;
			$this->stl = 0;
		}
			
			$this->pts = $this->calculatePts();
			$this->totalRbd = $this->calculateRbd();
			$this->BLindex = $this->calculateBLindex();
	}
	
	// functions
	
	private function getfromdb() {
		$q = "SELECT licne, ft, fg, 3pt, mft, mfg, m3p, orbd, drbd, blk, ast, stl FROM statistika WHERE mec = " . $this->mec . " AND igrac = " . $this->igrac;
		$r = mysql_query("set names 'utf8'");
		$r = mysql_query($q);
		if (mysql_num_rows($r)) {
			return (mysql_fetch_array($r));
		}
	}
	
	public function calculatePts() {
		return ($this->ft + $this->fg * 2 + $this->threept * 3);
	}
	
	public function calculateRbd() {
		return ($this->drbd + $this->orbd);
	}
	
	public function calculateBLindex() {
		$ind = $this->pts + $this->totalRbd + $this->ast + $this->blk + $this->stl - $this->licne - ($this->mft + $this->mfg + $this->mtp);
		return $ind;
	}

	// admin
	
	public function insertIntoDb() {
		$q = "INSERT INTO statistika (mec, igrac, licne, ft, 3pt, orbd, drbd, blk, ast) VALUES ('".$this->mec."', '".$this->igrac."', '".$this->licne."', '".$this->ft."', '".$this->threept."', '".$this->orbd."', '".$this->drbd."', '".$this->blk."', '".$this->ast."')";
		$r = mysql_query("set names 'utf8'");
		if (mysql_query($q)) {
			// echo "Nova statistika je dodata u bazu podataka";
		}
	}
	
				
} // end of class Statistics


?>